package physgin;

import java.util.*;

/**
 * Controls all the bodies in a World.
 * Used to add and remove bodies from the world.
 * Call start to set everything in motion.
 */
public class BodyController {
    private List<Body> bodies;
    private World w;
    private Collisions c;
    
    /**
     * Default class constructor.
     *
     * @param   w   A World object to place the bodies in.
     * @see         World
     */
    public BodyController(World w)
    {
        bodies = new ArrayList<Body>();
        this.w = w;
        c = new Collisions(this);
    }
    
    /**
     * Starts the collision detection thread, which also
     * controls moving of the Body objects and calls
     * methods to draw them on screen.
     * 
     * @return      void
     */
    public synchronized void start()
    {
        (new Thread(c)).start();
    }
    
    /**
     * Adds a Body object to the world.
     *
     * @param   b   Body object to add to the world.
     * @return      void
     * @see         Body
     */
    public synchronized void addBody(Body b)
    {
        bodies.add(b);
        w.addBody(b);
    }
    
    /**
     * Removes a Body from the world.
     *
     * @param   b   Body object to remove.
     * @return      void
     * @see         Body
     */
    public synchronized void removeBody(Body b)
    {
        bodies.remove(b);
        w.removeBody(b);
    }
    
    /**
     * Returns the world as a World object.
     *
     * @return      A World object of the engines world.
     * @see         World
     */
    public synchronized World getWorld()
    {
        return w;
    }
    
    /**
     * Returns all the Body objects in the game in a List.
     * The list is unmodifiable.
     *
     * @return      A List<Body> of Body objects
     * @see         Body
     */
    synchronized List<Body> getBodies()
    {
        return Collections.unmodifiableList(bodies);
    }
}
